package com.z.dao.impl;

import com.z.dao.OrderDao;
import com.z.pojo.Cart;
import com.z.pojo.Order;
import com.z.utils.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class OrderDaoImpl implements OrderDao {
    private Connection conn;
    private PreparedStatement ps;

    @Override
    public Integer addOrder(Order order) throws SQLException {
        this.conn = DBUtil.getConnection();
        String sql="insert into `order` (oid,uid,otime,sprice,state,gid,gnum) values (?,?,?,?,?,?,?)";
        this.ps =conn.prepareStatement(sql);

        this.ps.setString(1,order.getOid());
        this.ps.setInt(2,order.getUid());
        this.ps.setString(3,order.getOtime());
        this.ps.setDouble(4,order.getSprice());
        this.ps.setString(5,order.getState());
        this.ps.setInt(6,order.getGid());
        this.ps.setInt(7,order.getGnum());
        int i = ps.executeUpdate();
        DBUtil.close(ps,conn);
        return i;
    }

    @Override
    public Integer addOrderItem(Cart cart, String oid) throws SQLException {
        this.conn = DBUtil.getConnection();
        String sql="insert into orderitem (pid,numb,sum,oid)" +
                " values (?,?,?,?)";
        this.ps =conn.prepareStatement(sql);
        this.ps.setInt(1,cart.getGid());
        this.ps.setInt(2,cart.getNum());
        this.ps.setDouble(3,cart.getPrice()*cart.getNum());
        this.ps.setString(4,oid);
        int i = ps.executeUpdate();
        DBUtil.close(ps,conn);
        return i;
    }

    @Override
    public Order findOrderByOid(String oid) throws SQLException {
        this.conn = DBUtil.getConnection();
        String sql ="select * from `order` where oid = ?";
        this.ps =conn.prepareStatement(sql);
        this.ps.setString(1,oid);
        ResultSet resultSet = ps.executeQuery();
        Order order =null;
        if (resultSet.next()){
            order = new Order();
            order.setUid(resultSet.getInt("uid"));
            order.setSprice(resultSet.getDouble("sprice"));
            order.setState(resultSet.getString("state"));
            order.setOtime(resultSet.getString("otime"));
            order.setOid(oid);
            order.setGid(resultSet.getInt("gid"));
            order.setGnum(resultSet.getInt("gnum"));
        }
        DBUtil.close(ps,conn);
        return order;
    }

    @Override
    public List<Order> findOrderByUid(String uid) throws SQLException {
        this.conn = DBUtil.getConnection();
        String sql ="select * from `order` where uid = ?";
        this.ps =conn.prepareStatement(sql);
        this.ps.setString(1,uid);
        ResultSet resultSet = ps.executeQuery();
        List<Order> orders = new ArrayList<>();
        Order order =null;
        while (resultSet.next()){
            order = new Order();
            order.setUid(resultSet.getInt("uid"));
            order.setSprice(resultSet.getDouble("sprice"));
            order.setState(resultSet.getString("state"));
            order.setOtime(resultSet.getString("otime"));
            order.setOid(resultSet.getString("oid"));
            order.setGid(resultSet.getInt("gid"));
            order.setGnum(resultSet.getInt("gnum"));
            orders.add(order);
        }
        DBUtil.close(ps,conn);
        return orders;
    }

    @Override
    public List<Cart> findOrderItemByOid(String oid) throws SQLException {
        this.conn = DBUtil.getConnection();
        String sql ="select * from order,goods  where oid = ? and order.pid = goods.gid";
        this.ps =conn.prepareStatement(sql);
        this.ps.setString(1,oid);
        ResultSet resultSet = ps.executeQuery();
        List<Cart> carts = new ArrayList<>();
        Cart  cart = null;
        while (resultSet.next()){
            cart = new Cart();
            cart.setNum(resultSet.getInt("num"));
            cart.setGid(resultSet.getInt("gid"));
            cart.setG_img(resultSet.getString("g_img"));
            cart.setGname(resultSet.getString("gname"));
            cart.setPrice(resultSet.getDouble("price"));
            carts.add(cart);
        }
        DBUtil.close(ps,conn);
        return carts;
    }

    @Override
    public Integer updateOrderByOid(String oid, Integer gid) throws SQLException {
        this.conn = DBUtil.getConnection();
        String sql="update `order` set gid = ?,ostatu ='已确认' where oid = ?";
        this.ps =conn.prepareStatement(sql);
        this.ps.setInt(1,gid);
        this.ps.setString(2,oid);

        int i = ps.executeUpdate();
        DBUtil.close(ps,conn);
        return i;
    }
}
